//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class RelatedProductDBManager
    {
        #region Methods
        public static void DeleteRelatedProduct(int RelatedProductID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_RelatedProductDelete");
            db.AddInParameter(dbCommand, "RelatedProductID", DbType.Int32, RelatedProductID);
            int retValue = db.ExecuteNonQuery(dbCommand);
        }

        public static RelatedProductCollection GetRelatedProductsByProductID1(int ProductID1, bool showHidden)
        {
            RelatedProductCollection relatedProductCollection = new RelatedProductCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_RelatedProductLoadByProductID1");
            db.AddInParameter(dbCommand, "ProductID1", DbType.Int32, ProductID1);
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    RelatedProduct relatedProduct = new RelatedProduct();
                    relatedProduct.RelatedProductID = NopDataHelper.GetInt(dataReader, "RelatedProductID");
                    relatedProduct.ProductID1 = NopDataHelper.GetInt(dataReader, "ProductID1");
                    relatedProduct.ProductID2 = NopDataHelper.GetInt(dataReader, "ProductID2");
                    relatedProduct.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    relatedProductCollection.Add(relatedProduct);
                }
            }

            return relatedProductCollection;
        }

        public static RelatedProduct GetByRelatedProductID(int RelatedProductID)
        {
            RelatedProduct relatedProduct = null;
            if (RelatedProductID == 0)
                return relatedProduct;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_RelatedProductLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "RelatedProductID", DbType.Int32, RelatedProductID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    relatedProduct = new RelatedProduct();
                    relatedProduct.RelatedProductID = NopDataHelper.GetInt(dataReader, "RelatedProductID");
                    relatedProduct.ProductID1 = NopDataHelper.GetInt(dataReader, "ProductID1");
                    relatedProduct.ProductID2 = NopDataHelper.GetInt(dataReader, "ProductID2");
                    relatedProduct.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                }
            }
            return relatedProduct;
        }

        public static RelatedProduct InsertRelatedProduct(int ProductID1, int ProductID2, int DisplayOrder)
        {
            RelatedProduct relatedProduct = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_RelatedProductInsert");
            db.AddOutParameter(dbCommand, "RelatedProductID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "ProductID1", DbType.Int32, ProductID1);
            db.AddInParameter(dbCommand, "ProductID2", DbType.Int32, ProductID2);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int RelatedProductID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@RelatedProductID"));
                relatedProduct = GetByRelatedProductID(RelatedProductID);
            }
            return relatedProduct;
        }

        public static RelatedProduct UpdateRelatedProduct(int RelatedProductID, int ProductID1, int ProductID2,
            int DisplayOrder)
        {
            RelatedProduct relatedProduct = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_RelatedProductUpdate");
            db.AddInParameter(dbCommand, "RelatedProductID", DbType.Int32, RelatedProductID);
            db.AddInParameter(dbCommand, "ProductID1", DbType.Int32, ProductID1);
            db.AddInParameter(dbCommand, "ProductID2", DbType.Int32, ProductID2);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                relatedProduct = GetByRelatedProductID(RelatedProductID);

            return relatedProduct;
        }
        #endregion
    }
}
